home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / scope / 001-025 / scopedisk8 / hunker / hunker.c < prev    next >
C/C++ Source or Header  |  1995-03-18  |  10KB  |  549 lines

  1. #include <stdio.h>
  2. #include <libraries/dosextens.h>
  3. #include <functions.h>
  4. #include "hunk.h"
  5. #include <exec/types.h>
  6.  
  7. #define    DC    SET_UNCHANGED        /* dont change flag */
  8.  
  9. int    first = 0, last = 1, current, listsize;
  10. struct    FileHandle *fd;
  11. ULONG    codemem=DC, datamem=DC, bssmem=DC, memtype(), *changelist = NULL;
  12. BOOL    verbose = FALSE;
  13.  
  14. main(argc, argv)
  15. int    argc;
  16. char    **argv;
  17. {
  18.     int    i = 2;
  19.  
  20.     if (argc < 3)
  21.     {
  22.         show_usage(argv[0]);
  23.     }
  24.     while (i < argc)
  25.     {
  26.         if (argv[i][0] == '-')
  27.         {
  28.             if (argv[i][1] == VERBOSE_FLAG) {
  29.                 verbose = TRUE;
  30.             }
  31.         }
  32.         else if ((argv[i][1] != '=') && (argv[i][0] != '-'))
  33.         {
  34.             show_usage(argv[0]);
  35.             exit(0);
  36.         }
  37.         else {
  38.             set_mem_flags(argv[i]);
  39.         }
  40.         i++;
  41.     }
  42.     if ((fd = Open(argv[1],MODE_OLDFILE))==NULL)
  43.     {
  44.         printf("Sorry, file: %s not found\n",argv[1]);
  45.         exit(10);
  46.     }
  47.     processfile();
  48.     fixhunk();
  49.     Exit();
  50. }
  51.  
  52. /***********************************************************************
  53. *
  54. *  ROUTINE:    memtype
  55. *
  56. *  IMPORTS:    char    type;        character after '=' in CLI arg
  57. *
  58. *  RETURNS:    ULONG    memtype;    memory specification long word
  59. *
  60. *  DESCRIPTION:    returns the mask for the type of memory specified by
  61. *        the character after the '=' in a CLI argument
  62. *
  63. ***********************************************************************/
  64.  
  65. ULONG    memtype(type)
  66. char    type;
  67. {
  68.     ULONG    settype;
  69.  
  70.     switch (type)
  71.     {
  72.     case    SPEC_CHIP:
  73.         settype = SET_CHIP;
  74.         break;
  75.  
  76.     case    SPEC_FAST:
  77.         settype    = SET_FAST;
  78.         break;
  79.  
  80.     case    SPEC_EITHER:
  81.         settype = SET_EITHER;
  82.         break;
  83.  
  84.     default:
  85.         settype = SET_UNCHANGED;
  86.     }
  87.     return(settype);
  88. }
  89.         
  90. /***********************************************************************
  91. *
  92. *  ROUTINE:    processfile
  93. *
  94. *  IMPORTS:    none
  95. *
  96. *  RETURNS:    none
  97. *
  98. *  DESCRIPTION:    reads through the file until the last hunk has bee processed
  99. *
  100. ***********************************************************************/
  101.  
  102. processfile()
  103. {
  104.     ULONG    hunk, readlong();
  105.  
  106.     while (first <= last)
  107.     {
  108.         hunk = (readlong());
  109.         if (verbose) {
  110.             printf("Hunk type %x\n", hunk);
  111.         }
  112.         processhunk(hunk);
  113.     }
  114. }
  115.  
  116. /***********************************************************************
  117. *
  118. *  ROUTINE:    readlong
  119. *
  120. *  IMPORTS:    NONE
  121. *
  122. *  RETURNS:    ULONG    data;    long word read from the file
  123. *
  124. *  DESCRIPTION:    reads the next long word in the file
  125. *
  126. ***********************************************************************/
  127.  
  128. ULONG    readlong()
  129. {
  130.     ULONG    hunk[2];
  131.     int    bytes;
  132.  
  133.     if ((bytes = Read(fd, hunk, sizeof(ULONG))) < sizeof(ULONG))
  134.     {
  135.         printf("could not read full long word\n");
  136.         Exit(0);
  137.     }
  138.     return(hunk[0]);
  139. }
  140.  
  141. /***********************************************************************
  142. *
  143. *  ROUTINE:    processhunk
  144. *
  145. *  IMPORTS:    ULONG    hunk;    hunk type
  146. *
  147. *  RETURNS:    NONE
  148. *
  149. *  DESCRIPTION:    calls the handler routine for the specified hunk type
  150. *
  151. ***********************************************************************/
  152.  
  153. processhunk(hunk)
  154. ULONG    hunk;
  155. {
  156.     switch (hunk)
  157.     {
  158.     case    HUNK_HEADER:
  159.         header_hunk();
  160.         break;
  161.  
  162.     case    HUNK_CODE:
  163.         code_hunk();
  164.         break;
  165.  
  166.     case    HUNK_RELOC8:
  167.     case    HUNK_RELOC16:
  168.     case    HUNK_RELOC32:
  169.         reloc_hunk();
  170.         break;
  171.  
  172.     case    HUNK_DEBUG:
  173.         debug_hunk();
  174.         break;
  175.  
  176.     case    HUNK_END:
  177.         first++;
  178.         break;
  179.  
  180.     case    HUNK_DATA:
  181.         data_hunk();
  182.         break;
  183.  
  184.     case    HUNK_BSS:
  185.         bss_hunk();
  186.         break;
  187.  
  188.     default    :
  189.         break;
  190.     }
  191. }
  192.  
  193. /***********************************************************************
  194. *
  195. *  ROUTINE:    fastforward
  196. *
  197. *  IMPORTS:    NONE
  198. *
  199. *  RETURNS:    NONE
  200. *
  201. *  DESCRIPTION:    advances <offset> long words through the file;
  202. *
  203. ***********************************************************************/
  204.  
  205. fastforward(offset)
  206. long    offset;
  207. {
  208.     ULONG    readlong();
  209.     int    oldpos, i;
  210.  
  211.     offset *= sizeof(ULONG);
  212.     oldpos = Seek(fd, offset, OFFSET_CURRENT);
  213. }
  214.  
  215. /***********************************************************************
  216. *
  217. *  ROUTINE:    fastback
  218. *
  219. *  IMPORTS:    NONE
  220. *
  221. *  RETURNS:    NONE
  222. *
  223. *  DESCRIPTION:    Backs up 1 long word in the file
  224. *
  225. ***********************************************************************/
  226.  
  227. fastback()
  228. {
  229.     int oldpos;
  230.  
  231.     oldpos = Seek(fd, -sizeof(ULONG), OFFSET_CURRENT);
  232. }
  233.  
  234. /***********************************************************************
  235. *
  236. *  ROUTINE:    header_hunk
  237. *
  238. *  IMPORTS:    none
  239. *
  240. *  RETURNS:    none
  241. *
  242. *  DESCRIPTION:    Processes the header hunk and allocates enough memory
  243. *        for the hunk table.
  244. *
  245. ***********************************************************************/
  246.  
  247. header_hunk()
  248. {
  249.     ULONG    chars_in_name, size, readlong();
  250.     int    oldposition, i;
  251.     long    offset;
  252.  
  253.     chars_in_name = readlong();
  254.     while (chars_in_name)
  255.     {
  256.         offset = chars_in_name;
  257.         fastforward(offset);
  258.         chars_in_name = readlong();
  259.     }
  260.     size = readlong();
  261.     first = readlong();
  262.     last = readlong();
  263.     offset = 0;
  264.     for (i = first; i <= last; i++)
  265.     {
  266.         offset += readlong();
  267.     }
  268.     changelist = (ULONG *)AllocMem(size * sizeof(ULONG), 0);
  269.     listsize = size;
  270.     current = 0;
  271. }
  272.  
  273. /***********************************************************************
  274. *
  275. *  ROUTINE:    code_hunk
  276. *
  277. *  IMPORTS:    NONE
  278. *
  279. *  RETURNS:    NONE
  280. *
  281. *  DESCRIPTION:    processes all code hunks
  282. *
  283. ***********************************************************************/
  284.  
  285. code_hunk()
  286. {
  287.     ULONG    size, temp, readlong();
  288.  
  289.     changelist[current++] = codemem;
  290.     size = readlong();
  291.     fastforward(size);
  292. }
  293.  
  294. /***********************************************************************
  295. *
  296. *  ROUTINE:    reloc_hunk
  297. *
  298. *  IMPORTS:    NONE
  299. *
  300. *  RETURNS:    NONE
  301. *
  302. *  DESCRIPTION:    handles all reloc32 hunks
  303. *
  304. ***********************************************************************/
  305.  
  306. reloc_hunk()
  307. {
  308.     ULONG    num, garb, readlong();
  309.     int    i;
  310.  
  311.     num = readlong();
  312.     while (num != 0)
  313.     {
  314.         num++;
  315.         fastforward(num);
  316.         num = readlong();
  317.     }
  318. }
  319.  
  320. /***********************************************************************
  321. *
  322. *  ROUTINE:    debug_hunk
  323. *
  324. *  IMPORTS:    NONE
  325. *
  326. *  RETURNS:    NONE
  327. *
  328. *  DESCRIPTION:    handles all debug hunks
  329. *
  330. ***********************************************************************/
  331.  
  332. debug_hunk()
  333. {
  334.     ULONG    num, readlong();
  335.  
  336.     num = readlong();
  337.     if (num >0)
  338.     {
  339.         fastforward(num);
  340.     }
  341. }
  342.  
  343. /***********************************************************************
  344. *
  345. *  ROUTINE:    data_hunk
  346. *
  347. *  IMPORTS:    NONE
  348. *
  349. *  RETURNS:    NONE
  350. *
  351. *  DESCRIPTION:    processes all data hunks.
  352. *
  353. ***********************************************************************/
  354.  
  355. data_hunk()
  356. {
  357.     ULONG    readlong();
  358.     long    num, check_len;
  359.     BOOL    test_string();
  360.  
  361.     num = readlong();
  362.     changelist[current++] = datamem;
  363.     fastforward(num);
  364. }
  365.  
  366. /***********************************************************************
  367. *
  368. *  ROUTINE:    bss_hunk
  369. *
  370. *  IMPORTS:    none
  371. *
  372. *  RETURNS:    none
  373. *
  374. *  DESCRIPTION:    handles all uninitiallized data hunks
  375. *
  376. ***********************************************************************/
  377.  
  378. bss_hunk()
  379. {
  380.     ULONG    num, readlong();
  381.  
  382.     changelist[current++] = bssmem;
  383.     num = readlong();
  384. }
  385.  
  386. /***********************************************************************
  387. *
  388. *  ROUTINE:    newtype
  389. *
  390. *  IMPORTS:    ULONG    type;    type of memory hunk is to be loaded into
  391. *
  392. *  RETURNS:    NONE
  393. *
  394. *  DESCRIPTION:    Changes the specification for where a hunk is to be
  395. *        loaded into memory.
  396. *
  397. ***********************************************************************/
  398.  
  399. newtype(type)
  400. ULONG    type;
  401. {
  402.     ULONG    readlong(), oldval, wrote, newval;
  403.  
  404.     oldval = readlong();
  405.     if (type != SET_UNCHANGED)
  406.     {
  407.         fastback();
  408.         oldval &= SET_MASK;
  409.         newval = oldval | type;
  410.         if (verbose) {
  411.             printf("changing %x to %x\n", oldval, newval);
  412.         }
  413.         wrote = Write(fd, &newval, sizeof(ULONG));
  414.         if (wrote != sizeof(ULONG))
  415.         {
  416.             printf("did not write full hunk label\n");
  417.             Exit();
  418.         }
  419.     }
  420. }
  421.  
  422. /***********************************************************************
  423. *
  424. *  ROUTINE:    Exit
  425. *
  426. *  IMPORTS:    NONE
  427. *
  428. *  RETURNS:    NONE
  429. *
  430. *  DESCRIPTION:    Frees memory, closes file, exits program.
  431. *
  432. ***********************************************************************/
  433.  
  434. Exit()
  435. {
  436.     int    i;
  437.  
  438.     if (changelist != NULL)
  439.     {
  440.         FreeMem(changelist, listsize);
  441.         changelist = NULL;
  442.     }
  443.     Close(fd);
  444.     exit(0);
  445. }
  446.  
  447. /***********************************************************************
  448. *
  449. *  ROUTINE:    fixhunk
  450. *
  451. *  IMPORTS:    none
  452. *
  453. *  RETURNS:    none
  454. *
  455. *  DESCRIPTION:    read through the table of hunk definitions made by this
  456. *        program and calls newtype() for all hunks that have been
  457. *        changed
  458. *
  459. ***********************************************************************/
  460.  
  461. fixhunk()
  462. {
  463.     ULONG    chars_in_name, size, readlong();
  464.     int    oldposition, i;
  465.     long    offset;
  466.  
  467.     oldposition = Seek(fd, 4L, OFFSET_BEGINNING);
  468.     chars_in_name = readlong();
  469.     while (chars_in_name)
  470.     {
  471.         offset = chars_in_name;
  472.         fastforward(offset);
  473.         chars_in_name = readlong();
  474.     }
  475.     size = readlong();
  476.     size = readlong();
  477.     size = readlong();
  478.     for (i = 0; i < current; i++)
  479.     {
  480.         newtype(changelist[i]);
  481.     }
  482. }
  483.  
  484. /***********************************************************************
  485. *
  486. *  ROUTINE:    set_mem_flags
  487. *
  488. *  IMPORTS:    char    *arg;    CLI argument to be interpreted
  489. *
  490. *  RETURNS:    none
  491. *
  492. *  DESCRIPTION:    interprets a hunk memory specification
  493. *
  494. ***********************************************************************/
  495.  
  496. set_mem_flags(arg)
  497. char    *arg;
  498. {
  499.     switch(arg[0])
  500.     {
  501.     case    SPEC_DATA:
  502.         datamem = memtype(arg[2]);
  503.         break;
  504.  
  505.     case    SPEC_CODE:
  506.         codemem = memtype(arg[2]);
  507.         break;
  508.  
  509.     case    SPEC_BSS:
  510.         bssmem = memtype(arg[2]);
  511.         break;
  512.     }
  513. }
  514.  
  515. /***********************************************************************
  516. *
  517. *  ROUTINE:    show_usage
  518. *
  519. *  IMPORTS:    char    *name;    program name
  520. *
  521. *  RETURNS:    none
  522. *
  523. *  DESCRIPTION:    shows usage for the program
  524. *
  525. ***********************************************************************/
  526.  
  527. show_usage(name)
  528. char    *name;
  529. {
  530.     printf("USAGE: %s <file> [-v] [c,d,b]=[c,f,e]\n", name);
  531.     printf("\n-v    sets verbose mode on\n");
  532.     printf("[c/d/b]    specify which hunks are to be altered.\n");
  533.     printf("    c - code, d - data, b - uninitaillized data.\n");
  534.     printf("[c/f/e]    specify where the altered hunks are to load.\n");
  535.     printf("    c - chip, f - fast, e - doesn't matter\n");
  536.     printf("\nNote:    changing two or more hunk types requires that the\n");
  537.     printf("    specification argument be repeated for each hunk\n");
  538.     printf("    type.  ie to load code into fast memory, and data\n");
  539.     printf("    into chip memory, the command would be\n");
  540.     printf("    %s <file> c=f d=c\n", name);
  541.     printf("\nYou may distribute this program free of charge to anyone\n");
  542.     printf("as long as: the README file is distributed with it, and it\n");
  543.      printf("is not part of a commercial product.\n\n");
  544.     printf("If you use this program and it saves you several hours of\n");
  545.     printf("work, I would appreciate any contribution\n");
  546.      printf("    Terry Fisher, Jan 17 1988\n");
  547.     exit(0);
  548. }
  549.